define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        Monkey = require("./monkey"),
        config = app.config,
        method = app.method;
    require("./jq_pagination");
    var item_tmp = require("./template/item.html");
    var item_tmp2 = require("./template/item2.html");
    template.helper('isselect', function(id, ids) {
        return $.inArray(id, ids.split(",")||[]) < 0 ? "" : "active";
    });
    exports.init = function(el,button) {
        var SelecteFun = function(opts) {
            this.opts = $.extend(true, {
                modal: null,
                type: null,
                select: null,
                goodsurl: "/Plus/selectgoods.php"
            }, opts || {});
            this.init();
        }

        SelecteFun.prototype = {
            init: function() {
                var _this = this;
                _this.create({
                    "page": 1
                });
                _this._click();
                _this.searchF();
            },
            create: function(searchData) {
                var _this = this;
                Monkey.post(_this.opts.goodsurl, function(d) {
                    if (config.issucceed(d)) {
                        if (_this.opts.type == window.goods_set.type) {
                            d.data.selectid = _this.opts.select.val();
                        }
                        $('#all-list-box').html(template.compile(item_tmp)(d.data));
                        var initPagination = function() {
                            // 创建分页
                            $("#Pagination").pagination(d.data.chief, {
                                num_edge_entries: 1, //边缘页数
                                num_display_entries: 4, //主体页数
                                callback: pageselectCallback,
                                items_per_page: 1, //每页显示1项
                                prev_text: "&laquo",
                                next_text: "&raquo",
                                load_first_page: true //第一次自动加载,true(默认),false(不加载)
                            });
                        };

                        var pageselectCallback = function(page_index, jq) {
                            //阻止单击事件
                            var data = $("form.search-form", _this.opts.modal).serialize();
                            data += "&page=" + (page_index+1) + "";
                            if (!$('#all-list-box div.list-box:eq(' + page_index + ')').data("crate")) {
                                Monkey.postd(_this.opts.goodsurl, function(d) {
                                    if (config.issucceed(d)) {
                                        if (_this.opts.type == window.goods_set.type) {
                                            d.data.selectid = _this.opts.select.val();
                                        }
                                        var $html = template.compile(item_tmp2)(d.data);
                                        $('#all-list-box').html($html);
                                    } else {
                                        config.msg.error(d.message);
                                    }
                                }, data);
                            }
                            return false;
                        }
                        initPagination();
                    } else {
                        config.msg.error(d.message);
                    }

                }, '', null, searchData);
            },
            _click: function() {},
            searchF: function() {
                var _this = this;
                $(document).on("click", "#goodssearch", function() {
                    var data = $("form.search-form", _this.opts.modal).serialize();
                    data += "&page=1";
                    _this.create(data);
                })
            }

        }
        function getGoddsIdMethod($t){
            var idval=[],ids=[];
            if($(".js_temp_goods_id",$t).val().length){
                idval=$(".js_temp_goods_id",$t).val().split(",");
            }
            if($(".js_temp_goods_id",$t).length){
                if(idval.length){
                    ids=idval
                }
            }else{
                ids=window.goods_set.selectid
            }
            return ids;
        }
        var tempid=[];
        var $wtmodal = $(button.data("tmodal")).length > 0 ? $(button.data("tmodal")) : $("#js_goodes_modal");
       
            var $t=$(button.data("target"),el);
             tempid = $.extend(true, [],getGoddsIdMethod($t));
            var $this = $(button),
                $tmodal = $($this.data("tmodal")).length > 0 ? $($this.data("tmodal")) : $("#js_goodes_modal"),
                type = $this.data("type"),
                $select = $($this.data("selectid")).length > 0 ? $($this.data("selectid")) : $("#js_selected_id"),
                $target = $this.data("target"),
                path = $this.data("path"),
                $trid = $("#js_tr_id"),
                trid = $this.data("id");
            $tmodal.attr("data-type", type);
            $tmodal.attr("data-target", $target);
            $tmodal.attr("data-select", $this.data("selectid") || "#js_selected_id");
            $tmodal.attr("data-logo", $this.data("logo"));
            var $input = $("#js_selected_id", $tmodal);
            $trid.length > 0 && $trid.val(trid);
            $input.val($(".js_goods_id", $t).val() || $this.data("goodsid"));
            if ($input.val() == "") {
                $input.val(getGoddsIdMethod($t));
            }
            $tmodal.modal("show");
            if (path) {
                new SelecteFun({
                    modal: $tmodal,
                    type: type,
                    button: $this,
                    select: $select,
                    goodsurl: path
                });
            }
        $wtmodal.unbind('click');
        $wtmodal.on("click", "[data-dismiss='modal']", function(e) {
            $wtmodal.modal("hide");
        });
        $wtmodal.on("click", "li.itmes-mul", function(e) {
            e.stopPropagation();
            var cid = $(this).data("cid") + "";
            var activeItme = $(this).find("a");
            activeItme.toggleClass("active");
            $(this).toggleClass("active");
            if ($(this).hasClass("active")) {
                if ($.inArray(cid, tempid) == -1) {
                    tempid.push(cid);
                }
            } else {
                tempid.splice($.inArray(cid, tempid), 1);
            }
            var $select = $($wtmodal.attr("data-select"));
            $select.val(tempid);
        });
        $wtmodal.on("click", "button.js_save_btn", function() {
            $wtmodal.modal("hide");
            var $select = $($wtmodal.attr("data-select")),
                target = $wtmodal.attr("data-target"),
                $logo = $($wtmodal.attr("data-logo")),
                type = $wtmodal.attr("data-type");
            $select.val(tempid);
            $(target,el).find("input[type='hidden']").val(tempid);
            if ($(".js_temp_goods_id", $(target,el)).length) {
                $(".js_temp_goods_id", $(target,el)).val(tempid);
            } else {
               window.goods_set.selectid = tempid;
            }
            
            /* $(tempid).each(function(){
                 var $goods=$(".goods_items_"+this),url=$goods.data("url"),
                 imgsrc=$goods.data("imgsrc"),logo=$goods.data("logo"),
                 text=$goods.data("text");
                 if(type=="url"){

                 }else if(type=="goods"){

                 }
             });*/
        });

    }
});